package bsserver;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;



/**
 * Lookup class to associate MIME types with file extensions.
 * @author marc
 */
public class MimeTypes {

    // correlates between extension type and mime type
    private HashMap<String, String> extensionType =
            new HashMap<String, String>();
    // valid mime types
    private HashMap<String, Boolean> validTypes =
            new HashMap<String, Boolean>();
    String lineIn;

    /**
     * Creates a new MimeTypes object by processing the given file for MIME
     * type/file extension associations.
     * @param fileName the filename of the file to process for MIME type
     * associations
     */
    public MimeTypes(String fileName) {

        try {
            BufferedReader input =
                    new BufferedReader(new FileReader(fileName));

            try {
                while ((lineIn = input.readLine()) != null) {

                    if (lineIn.trim().length() != 0 &&
                            !lineIn.trim().substring(0, 1).equals("#")) {
                        // replace multiple instances of whitespace
                        // with single whitespace, then split along
                        // that single whitespace.
                        String[] splitLine = lineIn.trim().
                                replaceAll("[ \t]{1,}", "\t").split("[ \t]");
                        if (splitLine.length > 1) {
                            // Store extension in hash pointing to mime type
                            for (int i = 1; i < splitLine.length; i++) {
                                extensionType.put(splitLine[i], splitLine[0]);
                            }
                        }

                        if (validTypes.containsKey(splitLine[0])) {
                            // no hard stops for config
                            System.out.println(
                                    "MIME.type: mime type already defined!: " +
                                    splitLine[0]);
                        } else {
                            validTypes.put(splitLine[0], true);
                        }
                    }
                }
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
            }
        } catch (Exception e) {
            System.out.println("File IO Error." + e);
            e.printStackTrace();
        }
    }

    /**
     * Checks to see if mimetype passed is in MIME.types,
     * with or without extension.
     * @param key mimetype to ceck
     * @return true if mimetype defined in MIME.type file
     */
    public boolean isValid(String key) {
        return validTypes.containsValue(key);
    }

    /**
     * Gets the mime type corresponding to the extension
     * @param key extension without the period
     * @return mime type corresponding to the passed extension.  Returns
     * null if there is no correspondence.
     */
    public String getType(String key) {
        return extensionType.get(key);
    }

    /**
     * Sets the mime type corresponding to given key.  can be used when
     * AddType directive encountered.  If extension already defined, it
     * is replaced with new definition.
     * @param extension extension to correspond to mime type
     * @param mimeType
     */
    public void setType(String extension, String mimeType) {
        extensionType.put(extension, mimeType);
        validTypes.put(mimeType, true);
    }
}
